package com.szxy.content.service.impl;

import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.szxy.content.service.ContentService;
import com.szxy.mapper.TbContentMapper;
import com.szxy.pojo.TbContent;
import com.szxy.pojo.TbContentExample;
import com.szxy.utils.ContentAD;
import com.szxy.utils.PageResult;
import com.szxy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Auther:zwer
 * @Date:2019/9/9 21:19
 * @Description:com.szxy.content.service
 * @Version:1.0
 **/
@Service
public class ContentServiceImpl implements ContentService {

    @Autowired
    private TbContentMapper contentMapper;

    @Override
    @LcnTransaction
    public int insertTbContent(TbContent content) {
        return this.contentMapper.insert(content);
    }

    @Override
    @LcnTransaction
    public Integer deleteContentByIds(Long ids) {
        return this.contentMapper.deleteByPrimaryKey(ids);
    }

    @Override
    public List<ContentAD> selectFrontendContentByAD() {
        TbContentExample example = new TbContentExample();
        TbContentExample.Criteria criteria = example.createCriteria();
        criteria.andCategoryIdEqualTo(89L);//89L 表示广告
        List<TbContent> contentList = this.contentMapper.selectByExampleWithBLOBs(example);
        //创建大广告 List 集合
        List<ContentAD>  contentADList = new ArrayList<>();
        for (TbContent tbContent : contentList) {
            ContentAD ad = new ContentAD();
            ad.setHeight(240);
            ad.setHeightB(240);
            ad.setWidth(670);
            ad.setWidthB(670);
            ad.setSrc(tbContent.getPic());
            ad.setSrcB(tbContent.getPic2());
            ad.setHref(tbContent.getUrl());
            ad.setAlt(tbContent.getContent());
            contentADList.add(ad);
        }
        return contentADList;
    }

    @Override
    public PageResult selectTbContentAllByCategoryId(Long categoryId, Integer page, Integer rows) {
        PageHelper.startPage(page,rows);
        TbContentExample example = new TbContentExample();
        TbContentExample.Criteria criteria = example.createCriteria();
        criteria.andCategoryIdEqualTo(categoryId);
        List<TbContent> contentList = this.contentMapper.selectByExample(example);
        PageInfo<TbContent> pageInfo = new PageInfo<>(contentList);
        PageResult pageResult = new PageResult();
        pageResult.setPageIndex(pageInfo.getPageNum());
        pageResult.setTotalPage(pageInfo.getPages());
        pageResult.setResult(pageInfo.getList());
        return pageResult;
    }
}
